﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

namespace map.swf
{
    public partial class thumbnail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            string id = Request.QueryString["id"] as string;
            if (id == null)
            {
                Response.StatusCode = 404;
                Response.Write("Not Found id is null");
                Response.End();
                return;
            }

            List<Thumbnail> thumbnails = Session["file_info"] as List<Thumbnail>;

            if (thumbnails == null)
            {
                Response.StatusCode = 404;
                Response.Write("Not Found thumbnails is null");
                Response.End();
                return;
            }                    
            foreach (Thumbnail thumb in thumbnails)
            {
                if (thumb.ID == id)
                {
                    try
                    {
                        string fileType = thumb.FileType;
                        bool isPicType = Thumbnail.IsPicType(fileType);
                        string fileName = string.Empty;
                        if (isPicType)
                            fileName = thumb.FileName;
                        else
                        {
                            if (Thumbnail.AllFileTypePicDict[fileType] != null)
                                fileName = Thumbnail.AllFileTypePicDict[fileType];
                        }
                        byte[] data;
                        if (thumb.Data != null)
                            data = thumb.Data;
                        else
                        {   
                            fileName = (fileName.Trim() == string.Empty) ? Server.MapPath(ResolveUrl("~/img/userdefaultimg.gif")) : Server.MapPath(ResolveUrl("~" + fileName));
                            StreamReader sReader = new StreamReader(fileName, Encoding.Default);
                            BinaryReader bReader = new BinaryReader(sReader.BaseStream);
                            byte[] picBuffer = new byte[sReader.BaseStream.Length];
                            bReader.Read(picBuffer, 0, (int)sReader.BaseStream.Length);
                            sReader.Close();
                            bReader.Close();
                            data = picBuffer;
                        }
                        Response.ContentType = Thumbnail.GetResponseConentType(fileType);
                        Response.BinaryWrite(data);
                        Response.End();
                                                           
                        return;
                    }
                    catch { }
                }
            }
            // If we reach here then we didn't find the file id so return 404
            Response.StatusCode = 404;
            Response.Write("Not Found");
            Response.End();
        }
    }
}
